package lec09api;

import java.util.Arrays;
import java.util.Comparator;

class Point implements Comparable<Point> {
    int x, y;

    Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public String toString() {
        return String.format("(%d,%d)", x, y);
    }

    @Override
    public int compareTo(Point that) {
        return this.x - that.x;//按照x坐标排升序
    }
}

public class ComparisonDemo {
    public static void main(String[] args) {
        Point[] points = new Point[10];
        for (int i = 0; i < points.length; i++) {
//            points[i] = new lec10api2.Point();
//            points[i].x = (int) (100 * Math.random());
//            points[i].y = (int) (100 * Math.random());
            points[i] = new Point((int) (100 * Math.random()), (int) (100 * Math.random()));
        }
        //Arrays.sort(points);
        Arrays.sort(points, new Comparator<Point>() {
            @Override
            public int compare(Point pa, Point pb) {
                return pa.y - pb.y;
            }
        });
        for (Point point : points) {
            System.out.println(point);
        }
    }
}
